package com.lujianfei.netease_android_touch

import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.TouchDelegate
import android.view.View
import android.widget.Toast
import com.lujianfei.netease_android_touch.utils.TouchDelegateComposite

class MainActivity : AppCompatActivity() {

    var button1: View? = null
    var button2: View? = null
    var touchDelegateComposite: TouchDelegateComposite? = null
    val offset = 300

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button1 = findViewById(R.id.button1)
        button2 = findViewById(R.id.button2)
        button1?.setOnClickListener {
            showToast("Button1 click")
        }
        button2?.setOnClickListener {
            showToast("Button2 click")
        }
        touchDelegateComposite = TouchDelegateComposite(button1?.parent as View)

        button1?.post {
            val rect = Rect()
            button1?.getHitRect(rect)
            rect.left -= offset
            rect.top -= offset
            rect.right += offset
            rect.bottom += offset
            touchDelegateComposite?.addDelegate(TouchDelegate(rect, button1))
        }

        button2?.post {
            val rect = Rect()
            button2?.getHitRect(rect)
            rect.left -= offset
            rect.top -= offset
            rect.right += offset
            rect.bottom += offset
            touchDelegateComposite?.addDelegate(TouchDelegate(rect, button2))
        }

        (button1?.parent as View).touchDelegate = touchDelegateComposite
    }

    fun showToast(text: String) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
    }
}
